$(function () {
    var menu_datagrid = $("#menu_datagrid");
    var menu_dialog = $("#menu_dialog");
    var menu_form = $("#menu_form");
    var menu_combotree = $("#menu_combotree");

    var mytree = $("#tree");



    //初始化菜单树
    mytree.tree({
        url:"/systemmenu/listRoot",
        fit:true,
        onClick:function (node) {
            //console.log(node);
            //mytree.tree("reload");
            var data = node.children;
            menu_datagrid.datagrid('loadData', data);
        },

    });






    var cmdObj = {
        //工具栏的刷新
        reload: function () {
            mytree.tree("reload");
            menu_datagrid.datagrid("reload");
        },
        //工具栏添加
        add: function () {
            //添加与编辑时
            menu_form.form("clear");
            //1.弹框
            menu_dialog.dialog("open");
            //设置上级菜单
            var node = mytree.tree('getSelected');
            if (node != null) {
                menu_combotree.combotree('setValue', node.id);
            }

            //设置标题
            menu_dialog.dialog("setTitle", "菜单添加");
        },
        //编辑
        edit: function () {
            //判断是否选中数据
            var row = menu_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', "请选择要编辑的数据");
                return;
            }
            console.log(row);
            //添加与编辑时将数据清空
            menu_form.form("clear");
            //console.log(row);
            //console.log(row.parent.text);
            menu_combotree.combotree('setValue', row.parent.id);
            //权限名字不一样, 需要修改
            if (row.permission) {
                row["permission.id"] = row.permission.id;
            }
            //数据回显: 同名原则
            menu_form.form("load", row);
            //1.弹框
            menu_dialog.dialog("open");
            //设置标题
            menu_dialog.dialog("setTitle", "菜单编辑");
        },

        //工具栏离职/复制
        remove: function () {
            //判断是否选中数据
            var row = menu_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', "请选择要编辑的数据");
                return;
            }
            $.messager.confirm('确认', '您确认想要执行这个操作吗？', function (r) {
                if (r) {
                    $.get("/systemmenu/delete", {id: row.id}, function (data) {
                        if (data.success) {
                            //刷新列表数据
                            menu_datagrid.datagrid("reload");
                        } else {
                            $.messager.alert('温馨提示', data.msg);
                        }
                    })
                }
            });
        },
        //返回顶级菜单
        backMenu:function () {
            menu_datagrid.datagrid("reload");
        },

        //保存操作
        save: function () {
            var id = $("#menuId").val();
            var url = "/systemmenu/save";
            if (id) {
                url = "/systemmenu/update";
            }
            menu_form.form('submit', {
                url: url,
                success: function (data) {
                    data = $.parseJSON(data);

                    if (!data.success) {
                        $.messager.alert('温馨提示', data.msg);
                    } else {
                        //关闭框
                        menu_dialog.dialog("close");

                        //刷新列表数据
                        menu_datagrid.datagrid("reload");
                    }
                }
            })
        },
        //关闭
        cancel: function () {
            menu_dialog.dialog("close");
        }

    }

    //调用
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        cmdObj[cmd]();
    });

    menu_datagrid.datagrid({
        url: "systemmenu/list",
        fit: true,
        fitColumns: true,
        striped: true,
        singleSelect: true,
        pagination: true,
        rownumbers: true,
        columns: [[
            {field: "ck", checkbox: true},//显示多选框样式
            {field: "id", hidden: true},
            {field: "text", title: "菜单名", width: 80},
            {field: "url", title: "URL", width: 80},
            {field: "parent", title: "父菜单", width: 80, formatter: function (value, row, index) {
                if (value) {
                    return value.text;
                } else {
                    return "";
                }
            }},
            {field: "permission", title: "绑定权限", width:80, formatter: function (value, row, index) {
                if (value) {
                    return value.resource;
                } else {
                    return "";
                }
            }}
        ]],
        toolbar: '#tb',
        onDblClickRow:function (index, row) {
            //console.log(row);
            var data = row.children;
            menu_datagrid.datagrid('loadData', data);

        }
    });
    menu_dialog.dialog({
        width: 400,
        height: 280,
        buttons: "#bb",
        closed: true
    });
});


